package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Address;
import com.fbee.smartcommunities.service.AddressService;
import com.fbee.smartcommunities.vo.ResultVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 收货地址 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-23
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/listByUid")
    public ResultVO<List<Address>> getList(Long openId){
        return ResultVO.success(addressService.list(new QueryWrapper<Address>().eq("user_id",openId)));
    }

    @PostMapping("/addAddress")
    public ResultVO<Boolean> addAddress(String name,String address,String phone,Long openId){
        return ResultVO.success(addressService.save(new Address(name,address,phone,openId)),"添加成功");
    }

    @PostMapping("/updateAddress")
    public ResultVO<Boolean> updateAddress(String name,String address,String phone,Long id){
        return ResultVO.success(addressService.update(null,new UpdateWrapper<Address>().eq("id",id).set("link_user",name).set("link_address",address).set("link_phone",phone)),"更新成功");
    }

    @PostMapping("/setDefault")
    public ResultVO<Boolean> setDefault(Long id){
        return ResultVO.success(addressService.setDefault(id));
    }

    @PostMapping("/deleteAddress")
    public ResultVO<Boolean> deleteAddress(Long id){
        return ResultVO.success(addressService.removeById(id),"删除成功");
    }

}
